//
// Created by yanhai on 2020/2/20.
//

#pragma once

#include <string>

namespace utility {
namespace string {

static std::vector<std::string>
split(const std::string &s, const char &c, std::string (*cb)(const std::string &) = nullptr)
{
    std::string buff{""};
    std::vector<std::string> v;

    for (auto n:s) {
        if (n != c) buff += n;
        else if (n == c && buff != "") {
            if (cb) {
                v.push_back(cb(buff));
            } else {
                v.push_back(buff);
            }

            buff = "";
        }
    }
    if (buff != "") {
        if (cb) {
            v.push_back(cb(buff));
        } else {
            v.push_back(buff);
        }
    }
    return v;
}

static std::vector<std::string>
split(const std::string &s, const std::string &c, std::string (*cb)(const std::string &) = nullptr)
{
    std::vector<std::string> v;
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while (std::string::npos != pos2) {
        if (cb) {
            v.push_back(cb(s.substr(pos1, pos2 - pos1)));
        } else {
            v.push_back(s.substr(pos1, pos2 - pos1));
        }

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length()) {
        if (cb) {
            v.push_back(cb(s.substr(pos1)));
        } else {
            v.push_back(s.substr(pos1));
        }
    }

    return v;
}

}
}
